package br.com.view;

import br.com.dao.ConectaBanco;
import br.com.dao.ModeloTabela;
import br.com.model.CamposCadFuncionario;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Cícero
 */
public class Frm_CadFuncionario extends javax.swing.JFrame {

    ConectaBanco conexao = new ConectaBanco();

    int codSetor, codFuncao;
    String setorFuncionario, funcaoFuncionario;

    /**
     * Creates new form Frm_CadFuncionario
     */
    public Frm_CadFuncionario() {
        initComponents();
        conexao.conexao();
        ComboFuncao();
        ComboSetor();        
        geraTabela("select * from cadastrofuncionario order by codigo_funcionario");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("bancosysepi?zeroDateTimeBehavior=convertToNullPU").createEntityManager();
        cadastrofuncionarioQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Cadastrofuncionario c");
        cadastrofuncionarioList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : cadastrofuncionarioQuery.getResultList();
        cadastrofuncionarioQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Cadastrofuncionario c");
        cadastrofuncionarioList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : cadastrofuncionarioQuery1.getResultList();
        cadastrofuncaoQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Cadastrofuncao c");
        cadastrofuncaoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : cadastrofuncaoQuery.getResultList();
        cadastrofuncaoQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Cadastrofuncao c");
        cadastrofuncaoList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : cadastrofuncaoQuery1.getResultList();
        btnNovo = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableFuncionario = new javax.swing.JTable();
        lblMatricula = new javax.swing.JLabel();
        textMatricula = new javax.swing.JTextField();
        lblNomeFun = new javax.swing.JLabel();
        textNome = new javax.swing.JTextField();
        lblNumeroPis = new javax.swing.JLabel();
        lblTurno = new javax.swing.JLabel();
        lblDataAdm = new javax.swing.JLabel();
        textDataAdmissao = new javax.swing.JFormattedTextField();
        lblCadFun = new javax.swing.JLabel();
        boxSetor = new javax.swing.JComboBox();
        boxFuncao = new javax.swing.JComboBox();
        lblSetor = new javax.swing.JLabel();
        lblFuncao = new javax.swing.JLabel();
        textNpis = new javax.swing.JFormattedTextField();
        lblCodigo = new javax.swing.JLabel();
        textCodigo = new javax.swing.JTextField();
        boxTurno = new javax.swing.JComboBox();
        btnPrimeiro = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnProximo = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SysEpi - Cadastro Funcionario");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon(42).png"))); // NOI18N
        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });
        getContentPane().add(btnNovo, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 430, -1, -1));

        btnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (7).png"))); // NOI18N
        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAlterar, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 430, -1, -1));

        btnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (3).png"))); // NOI18N
        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });
        getContentPane().add(btnExcluir, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 430, -1, -1));

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (4).png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(490, 430, -1, -1));

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (29).png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        getContentPane().add(btnSalvar, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 430, -1, -1));

        jScrollPane1.setViewportView(tableFuncionario);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 180, 599, 221));

        lblMatricula.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblMatricula.setText("Matricula");
        getContentPane().add(lblMatricula, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 70, -1, -1));

        textMatricula.setEnabled(false);
        getContentPane().add(textMatricula, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 70, 123, -1));

        lblNomeFun.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblNomeFun.setText("Nome");
        getContentPane().add(lblNomeFun, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 110, -1, -1));

        textNome.setEnabled(false);
        getContentPane().add(textNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 110, 329, -1));

        lblNumeroPis.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblNumeroPis.setText("Numero PIS");
        getContentPane().add(lblNumeroPis, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 140, -1, -1));

        lblTurno.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblTurno.setText("Turno");
        getContentPane().add(lblTurno, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 110, -1, -1));

        lblDataAdm.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblDataAdm.setText("Data Admissão");
        getContentPane().add(lblDataAdm, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 70, -1, -1));

        try {
            textDataAdmissao.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        textDataAdmissao.setEnabled(false);
        getContentPane().add(textDataAdmissao, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 70, 86, -1));

        lblCadFun.setFont(new java.awt.Font("Tahoma", 1, 22)); // NOI18N
        lblCadFun.setText("Cadastro Funcionário");
        getContentPane().add(lblCadFun, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 10, -1, -1));

        boxSetor.setEnabled(false);
        getContentPane().add(boxSetor, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 140, 146, -1));

        boxFuncao.setEnabled(false);
        boxFuncao.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                boxFuncaoItemStateChanged(evt);
            }
        });
        getContentPane().add(boxFuncao, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 140, 145, -1));

        lblSetor.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblSetor.setText("Setor");
        getContentPane().add(lblSetor, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 140, -1, -1));

        lblFuncao.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblFuncao.setText("Função");
        getContentPane().add(lblFuncao, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 140, -1, -1));

        try {
            textNpis.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.#####.##-#")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        textNpis.setEnabled(false);
        getContentPane().add(textNpis, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 140, 121, -1));

        lblCodigo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblCodigo.setText("Codigo");
        getContentPane().add(lblCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 70, -1, -1));

        textCodigo.setEnabled(false);
        getContentPane().add(textCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 70, 63, -1));

        boxTurno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manhã", "Tarde", "Noite" }));
        boxTurno.setEnabled(false);
        getContentPane().add(boxTurno, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 110, 119, -1));

        btnPrimeiro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (43).png"))); // NOI18N
        btnPrimeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeiroActionPerformed(evt);
            }
        });
        getContentPane().add(btnPrimeiro, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 430, -1, -1));

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (2).png"))); // NOI18N
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });
        getContentPane().add(btnAnterior, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 430, -1, -1));

        btnProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (24).png"))); // NOI18N
        btnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProximoActionPerformed(evt);
            }
        });
        getContentPane().add(btnProximo, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 430, -1, -1));

        btnUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (16).png"))); // NOI18N
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });
        getContentPane().add(btnUltimo, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 430, -1, -1));

        jSeparator1.setForeground(new java.awt.Color(0, 102, 102));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 48, 690, -1));

        jSeparator3.setForeground(new java.awt.Color(0, 102, 102));
        getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 414, 690, 10));

        setSize(new java.awt.Dimension(734, 525));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        NovoFuncionario();
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        Cancelar();
        conexao.desconecta();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        funcaoFuncionario = (String) boxFuncao.getSelectedItem();
        setorFuncionario = (String) boxSetor.getSelectedItem();
        buscaCodFuncao();
        buscaCodSetor();
        conexao.conexao();
        String sqlInserir = (" insert into cadastrofuncionario (matricula_funcionario, nome_funcionario, data_admissao_funcionario, numero_pis, turno_funcionario, idTBSetor, id_funcao) values (?,?,?,?,?,?,?) ");
        try {
            PreparedStatement pst = conexao.con.prepareStatement(sqlInserir);
            pst.setInt(1, Integer.parseInt(textMatricula.getText()));
            pst.setString(2, textNome.getText());
            pst.setDate(3, converteData(textDataAdmissao.getText()));
            pst.setString(4, textNpis.getText());
            pst.setString(5, (String) boxTurno.getSelectedItem());
            pst.setInt(6, codSetor);
            pst.setInt(7, codFuncao);
            pst.execute();
            pst.close();
            JOptionPane.showMessageDialog(null, "Funcionario inserido com Sucesso!");
            geraTabela("select * from cadastrofuncionario order by codigo_funcionario");
            conexao.desconecta();
            Cancelar();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Falha ao inserir o Funcionario!" + ex);
            conexao.desconecta();
        }
        //conexao.desconecta();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        conexao.conexao();
        String sqlExcluir = (" delete from cadastrofuncionario where codigo_funcionario ='" + this.textCodigo.getText() + "'");
        try {
            conexao.executaSQL(sqlExcluir);
            conexao.pst.close();
            JOptionPane.showMessageDialog(null, "Excluido com Sucesso!");
            geraTabela("select * from cadastrofuncionario order by codigo_funcionario");
            conexao.desconecta();
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao excluir" + erro);
            conexao.desconecta();
        }
        //conexao.desconecta();
    }//GEN-LAST:event_btnExcluirActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        conexao.conexao();
        String sqlAlterar = (" update cadastrofuncionario set (matricula_funcionario,nome_funcionario,data_admissao_funcionario,numero_pis,turno_funcionario) values (?,?,?,?,?,?)  where codigo_funcionario = ? ");
        try {
            PreparedStatement pst = conexao.con.prepareStatement(sqlAlterar);
            pst.setInt(1, Integer.parseInt(textMatricula.getText()));
            pst.setString(2, textNome.getText());
            pst.setDate(3, converteData(textDataAdmissao.getText()));
            pst.setString(4, textNpis.getText());
            pst.setString(5, (String) boxTurno.getSelectedItem());
            pst.setInt(6, (codFuncao));
            pst.setInt(7, (codSetor));
            pst.executeUpdate();
            pst.close();
            JOptionPane.showMessageDialog(null, "Funcionario alterado com Sucesso!");
            geraTabela("select * from cadastrofuncionario order by codigo_funcionario");
            conexao.desconecta();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Falha ao alterar o Funcionario!" + ex);
            conexao.desconecta();
        }
        //conexao.desconecta();
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void boxFuncaoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_boxFuncaoItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_boxFuncaoItemStateChanged

    private void btnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeiroActionPerformed
        NovoFuncionario();
        conexao.conexao();
        try {
            conexao.executaSQL("select * from cadastrofuncionario");
            conexao.rs.first();
            textCodigo.setText(String.valueOf(conexao.rs.getInt("codigo_funcionario")));
            textNome.setText(conexao.rs.getString("nome_funcionario"));
            textMatricula.setText(String.valueOf(conexao.rs.getInt("matricula_funcionario")));
            textNpis.setText(String.valueOf(conexao.rs.getInt("numero_pis")));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Erro Primeiro F"+ ex);
            conexao.desconecta();
        }
        conexao.desconecta();
    }//GEN-LAST:event_btnPrimeiroActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        conexao.conexao();
        try {
            conexao.rs.previous();
            textCodigo.setText(String.valueOf(conexao.rs.getInt("codigo_funcionario")));
            textNome.setText(conexao.rs.getString("nome_funcionario"));
            textMatricula.setText(String.valueOf(conexao.rs.getInt("matricula_funcionario")));
            textNpis.setText(String.valueOf(conexao.rs.getInt("numero_pis")));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Erro Anterios F"+ ex);
            conexao.desconecta();
        }
        conexao.desconecta();
    }//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProximoActionPerformed
        conexao.conexao();
        try {
            conexao.rs.next();
            textCodigo.setText(String.valueOf(conexao.rs.getInt("codigo_funcionario")));
            textNome.setText(conexao.rs.getString("nome_funcionario"));
            textMatricula.setText(String.valueOf(conexao.rs.getInt("matricula_funcionario")));
            textNpis.setText(String.valueOf(conexao.rs.getInt("numero_pis")));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Erro Proximo F"+ ex);
            conexao.desconecta();
        }
        conexao.desconecta();
    }//GEN-LAST:event_btnProximoActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        NovoFuncionario();
        conexao.conexao();
        try {
            conexao.executaSQL("select * from cadastrofuncionario");
            conexao.rs.last();
            textCodigo.setText(String.valueOf(conexao.rs.getInt("codigo_funcionario")));
            textNome.setText(conexao.rs.getString("nome_funcionario"));
            textMatricula.setText(String.valueOf(conexao.rs.getInt("matricula_funcionario")));
            textNpis.setText(String.valueOf(conexao.rs.getInt("numero_pis")));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex);
            conexao.desconecta();
        }
        conexao.desconecta();
    }//GEN-LAST:event_btnUltimoActionPerformed

    public void NovoFuncionario() {
        textMatricula.setEnabled(true);
        textDataAdmissao.setEnabled(true);
        textNome.setEnabled(true);
        textNpis.setEnabled(true);
        boxTurno.setEnabled(true);
        boxFuncao.setEnabled(true);
        boxSetor.setEnabled(true);
        textMatricula.requestFocus();
    }

    public void Cancelar() {
        textMatricula.setText("");
        textDataAdmissao.setText("");
        textNome.setText("");
        textNpis.setText("");
        boxTurno.setSelectedIndex(0);
        textMatricula.setEnabled(false);
        textDataAdmissao.setEnabled(false);
        textNome.setEnabled(false);
        textNpis.setEnabled(false);
        boxTurno.setEnabled(false);
        boxFuncao.setEnabled(false);
        boxSetor.setEnabled(false);
    }

    private void ComboFuncao() {
        conexao.conexao();        
        try {
            conexao.executaSQL("SELECT * FROM cadastrofuncao order by nome_funcao");
            conexao.rs.first();
            boxSetor.removeAllItems();
            do {
                boxFuncao.addItem(conexao.rs.getString("nome_funcao"));
            } while (conexao.rs.next());
            conexao.rs.close();
            //conexao.desconecta();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher ComboBoxSetor \n" + ex);
             conexao.desconecta();
        }
        conexao.desconecta();
    }

    private void ComboSetor() {
        conexao.conexao();        
        try {
            conexao.executaSQL("SELECT * FROM setor order by nome_setor");
            conexao.rs.first();
            boxSetor.removeAllItems();
            do {
                boxSetor.addItem(conexao.rs.getString("nome_setor"));
            } while (conexao.rs.next());
            conexao.rs.close();
            //conexao.desconecta();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher ComboBoxSetor \n" + ex);
            conexao.desconecta();
        }
        conexao.desconecta();
    }

    public void buscaCodSetor() {
        conexao.conexao();
        try {
            conexao.executaSQL("select * from setor where nome_setor = '" + this.setorFuncionario + "'");
            conexao.rs.first();
            codSetor = conexao.rs.getInt("idTBSetor");
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher ComboBoxSetor \n" + erro);
            conexao.desconecta();
        }
        conexao.desconecta();
    }

    public void buscaCodFuncao() {
        conexao.conexao();
        try {
            conexao.rs = conexao.pst.executeQuery("select * from cadastrofuncao where nome_funcao = '" + this.funcaoFuncionario + "'");
            conexao.rs.first();
            codFuncao = conexao.rs.getInt("id_funcao");
        } catch (SQLException erro) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher Função \n" + erro);
            conexao.desconecta();
        }
        conexao.desconecta();
    }

    private Date converteData(String data) {

        Date d = null;
        try {
            data = data.substring(6) + "-"
                    + data.substring(3, 5) + "-"
                    + data.substring(0, 2);
            d = Date.valueOf(data);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return d;
    }

    public void geraTabela(String SQL) {
        ArrayList dados = new ArrayList();
        String[] Colunas = new String[]{"Codigo", "Nome", "Data Admissão"};
        conexao.executaSQL(SQL);
        try {
            conexao.rs.first();
            do {
                dados.add((new Object[]{conexao.rs.getInt("codigo_funcionario"), conexao.rs.getString("nome_funcionario"), conexao.rs.getDate("data_admissao_funcionario"),}));
            } while (conexao.rs.next());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher o arrayList" + ex);
            conexao.desconecta();
        }
        ModeloTabela modeloTabela = new ModeloTabela(dados, Colunas);
        tableFuncionario.setModel(modeloTabela);
        tableFuncionario.getColumnModel().getColumn(0).setPreferredWidth(180);
        tableFuncionario.getColumnModel().getColumn(0).setResizable(false);
        tableFuncionario.getColumnModel().getColumn(1).setPreferredWidth(280);
        tableFuncionario.getColumnModel().getColumn(1).setResizable(false);
        tableFuncionario.getColumnModel().getColumn(2).setPreferredWidth(180);
        tableFuncionario.getColumnModel().getColumn(2).setResizable(false);

        tableFuncionario.getTableHeader().setReorderingAllowed(false);
        tableFuncionario.setAutoResizeMode(tableFuncionario.AUTO_RESIZE_OFF);
        tableFuncionario.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox boxFuncao;
    private javax.swing.JComboBox boxSetor;
    private javax.swing.JComboBox boxTurno;
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnPrimeiro;
    private javax.swing.JButton btnProximo;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JButton btnUltimo;
    private java.util.List<br.com.model.Cadastrofuncao> cadastrofuncaoList;
    private java.util.List<br.com.model.Cadastrofuncao> cadastrofuncaoList1;
    private javax.persistence.Query cadastrofuncaoQuery;
    private javax.persistence.Query cadastrofuncaoQuery1;
    private java.util.List<br.com.model.Cadastrofuncionario> cadastrofuncionarioList;
    private java.util.List<br.com.model.Cadastrofuncionario> cadastrofuncionarioList1;
    private javax.persistence.Query cadastrofuncionarioQuery;
    private javax.persistence.Query cadastrofuncionarioQuery1;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel lblCadFun;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblDataAdm;
    private javax.swing.JLabel lblFuncao;
    private javax.swing.JLabel lblMatricula;
    private javax.swing.JLabel lblNomeFun;
    private javax.swing.JLabel lblNumeroPis;
    private javax.swing.JLabel lblSetor;
    private javax.swing.JLabel lblTurno;
    private javax.swing.JTable tableFuncionario;
    private javax.swing.JTextField textCodigo;
    private javax.swing.JFormattedTextField textDataAdmissao;
    private javax.swing.JTextField textMatricula;
    private javax.swing.JTextField textNome;
    private javax.swing.JFormattedTextField textNpis;
    // End of variables declaration//GEN-END:variables
}
